/**
 * 写出一个用于虚拟机启动的磁盘镜像
 */

package edu.gzz.os;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import static java.lang.System.out;

public class OperatingSystem {
	private int[] imgContent = new int[] { 0xeb, 0x4e, 0x90, 0x48, 0x45, 0x4c, 0x4c, 0x4f, 0x49, 0x50, 0x4c, 0x00, 0x02,
			0x01, 0x01, 0x00, 0x02, 0xe0, 0x00, 0x40, 0x0b, 0xf0, 0x09, 0x00, 0x12, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x29, 0xff, 0xff, 0xff, 0xff, 0x48, 0x45, 0x4c, 0x4c, 0x4f, 0x2d,
			0x4f, 0x53, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x8e, 0xd0,
			0xbc, 0x00, 0x7c, 0x8e, 0xd8, 0x8e, 0xc0, 0xbe, 0x74, 0x7c, 0x8a, 0x04, 0x83, 0xc6, 0x01, 0x3c, 0x00, 0x74,
			0x09, 0xb4, 0x0e, 0xbb, 0x0f, 0x00, 0xcd, 0x10, 0xeb, 0xee, 0xf4, 0xeb, 0xfd };

	private ArrayList<Integer> imgBytes = new ArrayList<Integer>();

	public OperatingSystem(String s) {
		
		out.println("imgContent.length="+imgContent.length);
		
		for (var i = 0; i < imgContent.length; i++) {
			imgBytes.add(imgContent[i]);
		}

		imgBytes.add(0x0a);
		imgBytes.add(0x0a);
		
		for (var j = 0; j < s.length(); j++) {
			imgBytes.add((int) s.charAt(j));
		}
		
		imgBytes.add(0x0a);

		int len = 0x1fe;
		int curSize = imgBytes.size();
		for (var k = 0; k < len - curSize; k++) {
			imgBytes.add(0);
		}

		// 0x1fe-0x1f: 0x55, 0xaa
		// 0x200-0x203: f0 ff ff
		imgBytes.add(0x55);
		imgBytes.add(0xaa);
		imgBytes.add(0xf0);
		imgBytes.add(0xff);
		imgBytes.add(0xff);

		len = 0x168000;
		curSize = imgBytes.size();
		
		for (var l = 0; l < len - curSize; l++) {
			imgBytes.add(0);
		}

	}

	///生成软件镜像
	public void makeFllopy() {
		try {
			@SuppressWarnings("resource")
			DataOutputStream out = new DataOutputStream(new FileOutputStream("system.img"));
			for (int i = 0; i < imgBytes.size(); i++) {
				out.writeByte(imgBytes.get(i).byteValue());
			}
		} catch (Exception e) {
			// 
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		OperatingSystem op = new OperatingSystem("hello, this is my first line of my operating system code");
		op.makeFllopy();
		out.println("done");
	}
}